<template>
  <div>
    <el-card style="text-align:center">
      <h3>{{ name }}导入</h3>
      <ReadExcel :onSuccess="onSuccess"></ReadExcel>
    </el-card>
  </div>
</template>

<script>
import { sysUserBatch } from '@/api/employees.js'
export default {
  data () {
    return {
      name: this.$route.query.name
    }
  },
  methods: {
    async onSuccess ({ header, results }) {
      console.log(header, results)
      //   数据转换
      const obj = {
        手机号: 'mobile',
        姓名: 'username',
        入职日期: 'timeOfEntry',
        转正日期: 'correctionTime',
        工号: 'workNumber'
      }
      const newArr = results.map(item => {
        const temp = {}
        Object.keys(item).forEach(item2 => {
          temp[obj[item2]] = item[item2]
          if (obj[item2] === 'timeOfEntry' || obj[item2] === 'correctionTime') {
            const t1 = new Date((item[item2] - 1) * 24 * 3600000 - 8 * 3600000)
            t1.setYear(t1.getFullYear() - 70)
            temp[obj[item2]] = t1
          }
        })
        return temp
      })
      console.log(newArr)
      await sysUserBatch(newArr)
      this.$message.success('导入成功')
      this.$router.push(this.$route.query.redirect || '/employees')
    }
  }
}
</script>

<style></style>
